package com.scqzy.shengchunqiang.sword2offer.day12;

import com.scqzy.shengchunqiang.structure.ListNode;

import java.util.Objects;

/**
 * @Description: 剑指 Offer 52. 两个链表的第一个公共节点
 * @Author 盛春强
 * @Date 2021/8/19 17:39
 */
public class GetIntersectionNode {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode a = headA;
        ListNode b = headB;
        // a+c+b = b+c+a
        while (a != b) {
            a = Objects.isNull(a) ? headB : a.next;
            b = Objects.isNull(b) ? headA : b.next;
        }
        return a;
    }
}
